/* this is javascript instict of search simple ( combobox multiple select, slider range, like facebook and back to top */
$(document).ready(function() {
	
	var isOpenValue = $('#isOpenValue').val();
	
	if(isOpenValue != null && isOpenValue === 'true'){
		openSfMenu();
	}else{
		closeSfMenu();
	}
	
	/*-- init combobox multiselect --*/
    $('#example39').multiselect({
    	includeSelectAllOption: true,
        buttonClass: 'dropdown-toggle btn btn-default sfInputSearchContainer rounded col-md-12 col-sm-12 col-xs-12',
        numberDisplayed: 12,
        buttonContainer: '<span class="btn-group col-md-5" />',
        buttonText: function(options, select) {
            if (options.length == 0) {
                return 'Bạn tìm quận nào?' + ' <b class="caret"></b>';
            }
            else {
                if (options.length > this.numberDisplayed) {
                    return options.length + ' ' + this.nSelectedText + ' <b class="caret"></b>';
                }
                else {
                    var selected = '';
                    options.each(function() {
                        var label = ($(this).attr('label') !== undefined) ? $(this).attr('label') : $(this).html();
 
                        selected += label + ', ';
                    });
                    return selected.substr(0, 20) + ' <b class="caret"></b>';
                }
            }
        }
    });
    /*--/init combobox multiselect --*/
    /*-- like facebook component */
    scrollVariable = false;
    //stick in the fixed 100% height behind the navbar but don't wrap it
    $('#slide-nav.navbar .container').append($('<div id="navbar-height-col"></div>'));

    // Enter your ids or classes
    var toggler = '.navbar-toggle';
    var navigationwrapper = '#top-navbar-toggle';
    var slidewidth = '80%';
    var menuneg = '-100%';

    $("#slide-nav").on("click", toggler, function (e) {

        var selected = $(this).hasClass('slide-active');

        if(!selected){
        	$("html, body").animate({ scrollTop: 0 }, 1000);
        }
        
        $('#slidemenu').stop().animate({
            left: selected ? menuneg : '0px'
        });

        $(navigationwrapper).stop().animate({
            left: selected ? '0px' : slidewidth
        });

        $(this).toggleClass('slide-active', !selected);
        $('#slidemenu').toggleClass('slide-active');

        $('.navbar').toggleClass('slide-active');

    });

    var selected = '#slidemenu, #page-content, body, .navbar, .navbar-header';


    $(window).on("resize", function () {
        if ($(window).width() > 767 && $('#top-navbar-toggle').is(':hidden')) {
            $(selected).removeClass('slide-active');
        }
    });

    $(window).scroll(function(){
        if ($(this).scrollTop() > 100) {
            $('#backToTop').fadeIn('slow');
            if(scrollVariable == false){
	        	var container_width = $('.container').width();
		        var likeFaceBookIframe = '<iframe src="http://www.facebook.com/plugins/likebox.php?href=https://www.facebook.com/congdongvnexpress&amp;width=' + container_width + '&amp;height=214&amp;colorscheme=light&amp;show_faces=true&amp;border_color&amp;stream=false&amp;header=true&amp;appId=145923738846814" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:214px;" allowtransparency="true"></iframe>';
		
		        $('#likeFaceBookComponent').html(likeFaceBookIframe);
		        scrollVariable = true;
	        }
        } else {
            $('#backToTop').fadeOut('slow');
        }
    });
    /*-- /like facebook component */
    /* -- back to top */
    $('#backToTop').click(function(){
        $("html, body").animate({ scrollTop: 0 }, 2000);
        return false;
    });
    /* -- /back to top */
    
    $( "#openSearchFilter" ).on( "click", function() {
	    $("#openSearchFilter").hasClass("open")?closeSfMenu():openSfMenu();
	  });
}); /*-- /document ready */

/* slider range */
$(function() {
    $( "#slider-range" ).slider({
      range: true,
      min: 100,
      max: 1000,
      values: [ 200, 500 ],
      slide: function( event, ui ) {
        $( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
      }
    });
    $( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
      " - $" + $( "#slider-range" ).slider( "values", 1 ) );
  });

function advancedSearch(urlString){
    var value = $( "#slider-range" ).slider( "values", 0 );
    var value1 = $( "#slider-range" ).slider( "values", 1 );
    
    var form = new FormData();
    form.append("minCost", value);
    form.append("maxCost", value1);
    if($('#example39').val() != null){
    	form.append("idDistricts", $('#example39').val());
    }
    
	$.ajax({
		url: urlString + "/getRoomsSearchResult",
		type:"POST",
		data: form,
		dataType: 'text',
	    processData: false,
	    contentType: false,
   		beforeSend: function (xhr){ 
   	        xhr.setRequestHeader($("meta[name='_csrf_header']").attr("content"), $("meta[name='_csrf']").attr("content")); 
   	    },
		success: function (response) {
			document.write(response);
		},
		error: function(jqXHR, textStatus, errorThrown){
			alert(textStatus);
		}
	});
};

/* function for simple close and open */
function closeSfMenu(){
    $(".simplesearch").hide();
    $("#openSearchFilter").removeClass("open");
    $("#openSearchFilter").removeClass("my_search");
    var value = "isOpen=false";
    $.ajax({
			url: "asyncSessionVariable",
			type:"GET",
		    processData: false,
		    data: value,
		    contentType: false,
			success: function (response) {
				myMapJavaScriptFavourites = response;
		},
			error: function(jqXHR, textStatus, errorThrown){
				alert(textStatus);
			}
		});
}
function openSfMenu(){
    $(".simplesearch").show();
    $("#openSearchFilter").addClass("open");
    $("#openSearchFilter").addClass("my_search");
    var value = "isOpen=true";
    $.ajax({
		url: "asyncSessionVariable",
		type:"GET",
	    processData: false,
	    data: value,
	    contentType: false,
		success: function (response) {
			myMapJavaScriptFavourites = response;
	},
		error: function(jqXHR, textStatus, errorThrown){
			alert(textStatus);
		}
	});
}

/*  display like Facebook component resize--*/
$( window ).resize(function() {
    var container_width = $('.container').width();
    var likeFaceBookIframe = '<iframe src="http://www.facebook.com/plugins/likebox.php?href=https://www.facebook.com/congdongvnexpress&amp;width=' + container_width + '&amp;height=214&amp;colorscheme=light&amp;show_faces=true&amp;border_color&amp;stream=false&amp;header=true&amp;appId=145923738846814" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:214px;" allowtransparency="true"></iframe>';

    $('#likeFaceBookComponent').html(likeFaceBookIframe);
});
/*  /display like Facebook component resize --*/